<link rel="stylesheet" href="__STATIC__/lib/lay-module/treetable-lay/treetable.css?v={:time()}" media="all">
<style>
    .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
        height: 34px;
        line-height: 34px;
        padding: 0 8px;
    }
</style>
<div class="layuimini-container">
    <div class="layuimini-main">
        <table id="currentTable" class="layui-table layui-hide"
               data-auth-add="{:auth('goods.category/add')}"
               data-auth-edit="{:auth('goods.category/edit')}"
               data-auth-delete="{:auth('goods.category/delete')}"
               lay-filter="currentTable">
        </table>
    </div>
</div>
<script type="text/html" id="toolbar">
    <button class="layui-btn layui-btn-sm layuimini-btn-primary" data-treetable-refresh><i class="fa fa-refresh"></i>
    </button>
    <button class="layui-btn layui-btn-normal layui-btn-sm {if !auth('goods.category/add')}layui-hide{/if}"
            data-open="goods.category/add" data-title="添加" data-full="true"><i class="fa fa-plus"></i> 添加
    </button>
    <button class="layui-btn layui-btn-sm layui-btn-danger {if !auth('goods.category/del')}layui-hide{/if}"
            data-url="goods.category/del" data-treetable-delete="currentTableRenderId"><i class="fa fa-trash-o"></i> 删除
    </button>
</script>

<script type="text/javascript">

    require(["jquery", "easy-admin", "treetable", "iconPickerFa", "autocomplete"], function ($, ea) {

        var table = layui.table,
            treetable = layui.treetable,
            iconPickerFa = layui.iconPickerFa,
            autocomplete = layui.autocomplete;

        var init = {
            table_elem: '#currentTable',
            table_render_id: 'currentTableRenderId',
            index_url: 'goods.category/index',
            add_url: 'goods.category/add',
            delete_url: 'goods.category/delete',
            edit_url: 'goods.category/edit',
            modify_url: 'goods.category/modify',
        };


        var renderTable = function () {
            layer.load(2);
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                homdPid: 99999999,
                treeIdName: 'id',
                treePidName: 'parent_id',
                url: ea.url(init.index_url),
                elem: init.table_elem,
                id: init.table_render_id,
                toolbar: '#toolbar',
                page: false,
                skin: 'line',

                // @todo 不直接使用ea.table.render(); 进行表格初始化, 需要使用 ea.table.formatCols(); 方法格式化`cols`列数据
                cols: ea.table.formatCols([[
                    {type: 'checkbox'},
                    {field: 'name', minWidth: 200, title: '分类名称', search: false, align: 'left'},
                    {field: 'thumb', minWidth: 200, title: '封面', search: false, templet: ea.table.image},
                    {field: 'level', minWidth: 200, title: '分类等级', search: false, },
                    {field: 'sort', minWidth: 100, title: '分类排序', search: false,  edit: 'text'},
                    // {field: 'href', minWidth: 120, title: '菜单链接'},
                    // {
                    //     field: 'is_home',
                    //     width: 80,
                    //     title: '类型',
                    //     templet: function (d) {
                    //         if (d.pid === 99999999) {
                    //             return '<span class="layui-badge layui-bg-blue">首页</span>';
                    //         }
                    //         if (d.pid === 0) {
                    //             return '<span class="layui-badge layui-bg-gray">模块</span>';
                    //         } else {
                    //             return '<span class="layui-badge-rim">菜单</span>';
                    //         }
                    //     }
                    // },
                    // {field: 'status', title: '状态', width: 85, templet: ea.table.switch},
                    // {field: 'sort', width: 80, title: '排序', edit: 'text'},
                    {
                        minWidth: 200,
                        title: '操作',
                        templet: ea.table.tool,
                        operat: [
                            [{
                                text: '添加下级',
                                url: init.add_url,
                                method: 'open',
                                auth: 'add',
                                class: 'layui-btn layui-btn-xs layui-btn-normal',
                                extend: 'data-full="true"',
                            }, {
                                text: '编辑',
                                url: init.edit_url,
                                method: 'open',
                                auth: 'edit',
                                class: 'layui-btn layui-btn-xs layui-btn-success',
                                extend: 'data-full="true"',
                            }],
                            'delete'
                        ]
                    }
                ]], init),
                done: function () {
                    layer.closeAll('loading');
                }
            });
        };


        renderTable();

        $('body').on('click', '[data-treetable-refresh]', function () {
            renderTable();
        });

        $('body').on('click', '[data-treetable-delete]', function () {
            var tableId = $(this).attr('data-treetable-delete'),
                url = $(this).attr('data-url');
            tableId = tableId || init.table_render_id;
            url = url != undefined ? ea.url(url) : window.location.href;
            var checkStatus = table.checkStatus(tableId),
                data = checkStatus.data;
            if (data.length <= 0) {
                ea.msg.error('请勾选需要删除的数据');
                return false;
            }
            var ids = [];
            $.each(data, function (i, v) {
                ids.push(v.id);
            });
            ea.msg.confirm('确定删除？', function () {
                ea.request.post({
                    url: url,
                    data: {
                        id: ids
                    },
                }, function (res) {
                    ea.msg.success(res.msg, function () {
                        renderTable();
                    });
                });
            });
            return false;
        });

        ea.table.listenSwitch({filter: 'status', url: init.modify_url});

        ea.table.listenEdit(init, 'currentTable', init.table_render_id, true);

        ea.listen();


    });
</script>